package com.atom.common.remote.palace;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.atom.common.CommonConstant;
import com.atom.common.exception.AvensRemoteDatabaseDaoException;
import com.atom.common.util.CharSetUtil;
import com.atom.common.util.GlobalConfigReader;
import com.atom.common.util.HttpUtil;
import com.atom.common.util.TicketUtil;

@Service
public class NotifyUtil {

  private static Logger logger = Logger.getLogger(NotifyUtil.class);

  @Autowired(required = false) HttpSession session;

  private static final String HOST = GlobalConfigReader.getStringValue(CommonConstant.PALACE_SERVERNAME);

  public void notify(String msg, String userids, String creator, String orgname, int isurl) {
    Map<String, String> params = new HashMap<String, String>();
    final String METHOD = "msg.addMsgForEx.do";
    StringBuffer url = new StringBuffer(HOST);
    url.append(METHOD);    
    try {
      String finalUrl = TicketUtil.appendTicket(url, session);
      // 消息内容
      params.put("msg", msg);
      // 接受人id
      params.put("userids", userids);
      // 发送人名称
      params.put("creator", creator);
      // 发送人机构名称
      params.put("orgname", orgname);
      // 消息内容是否为链接
      params.put("isurl", isurl + "");
      String result = CharSetUtil.decodeUnicode(HttpUtil.post(finalUrl, params));
      logger.info("addMsgForEx result : " + result);
    } catch (Exception e) {
      e.printStackTrace();
      logger.error("error message = {}" + e.getMessage(), e);
      throw new AvensRemoteDatabaseDaoException("Notify Failed");
    }
  }

}
